package com.ithailong.reconsitutionmassion.dao.Impl;

import com.ithailong.reconsitutionmassion.common.Constants;
import com.ithailong.reconsitutionmassion.dao.UserDao;
import com.ithailong.reconsitutionmassion.entity.User;
import com.ithailong.reconsitutionmassion.exception.ServicesException;
import com.ithailong.reconsitutionmassion.util.Dbutils;

import org.springframework.stereotype.Repository;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * author:hlc
 * date:2023/10/28
 *
 * @author 17930
 */
@Repository
public class UserDaoImpl implements UserDao {
   private Dbutils dbHelper = new Dbutils();

   @Override
   public User selectUserByUsername(String username) throws ServicesException {
      String sql = "select username from kfm_user where username = ?";
      try {
         return dbHelper.selectOne(sql, User.class, username);
      } catch (SQLException e) {
         e.printStackTrace();
         return null;
      }
   }

   @Override
   public User selectUserByEmail(String email) {
      String sql = "select email from kfm_user where email = ?";
      try {
         return dbHelper.selectOne(sql, User.class, email);
      } catch (SQLException e) {
         e.printStackTrace();
         return null;
      }
   }

   /**
    * 用户登录
    *
    * @param username
    * @param password
    * @return
    * @throws ServicesException
    */
   @Override
   public User useLogin(String username, String password) throws ServicesException {
      String sql = "select id,username,name,role,avatar,email,status from kfm_user where username = ? and password = ?";
      try {
         return dbHelper.selectOne(sql, User.class, username, password);
      } catch (SQLException e) {
         e.printStackTrace();
         return null;
      }
   }

   /**
    * 用户注册
    *
    * @param user
    * @return
    */
   @Override
   public int register(User user) {
      String sql = "insert into kfm_user(username,password,name,email,role,status,create_time,code)" +
              "values(?,?,?,?,?,?,?,?)";
      try {
         return dbHelper.insert(sql, user.getUsername(), user.getPassword(),
                 user.getName(), user.getEmail(),
                 user.getRole(), user.getStatus(),
                 user.getCreateTime(), user.getCode());
      } catch (SQLException e) {
         e.printStackTrace();
         return 0;
      }
   }

   /**
    * 查询所有用户
    *
    * @return
    */
   @Override
   public List<User> userList(int pageNum, int pageSize, User user) {
      StringBuilder sql = new StringBuilder("select id,username,name,email,phone,role,status,create_time from kfm_user");
      StringBuilder where = new StringBuilder();//存放where条件
      List params = new ArrayList();//存放参数
      if (StringUtils.hasText(user.getName())) {//判断是否有昵称
         where.append(" where name = ? ");
         params.add(user.getName());
      }
      if (!ObjectUtils.isEmpty(user.getRole())) {//如果用户名不为空
         //先判断有没有将where进行拼接
         if (where.length() == 0) {
            where.append(" where role = ?");
         } else {
            where.append(" and role = ?");
         }
         params.add(user.getRole());
      }
      if (where.length() > 0) {
         sql.append(where);
      }
      sql.append(" limit ?,?");
      params.add((pageNum - 1) * pageSize);
      params.add(pageSize);
      System.out.println(params.size());
//      String sql = "select id,username,name,email,phone,role,status,create_time from kfm_user limit ?,?";
      try {
         return dbHelper.selectList(sql.toString(), User.class, params.toArray());
      } catch (SQLException e) {
         e.printStackTrace();
         return null;
      }
   }

   @Override
   public int selectTotal(User user) {
//      String sql = "select count(*) from kfm_user";
      StringBuilder sql = new StringBuilder("select count(*) from kfm_user ");
      StringBuilder where = new StringBuilder();
      List params = new ArrayList();
      if (StringUtils.hasText(user.getName())) {
         where.append(" where name = ?");
         params.add(user.getName());
      }
      if (!ObjectUtils.isEmpty(user.getRole())) {
         if (where.length() == 0) {
            where.append(" where role = ? ");
         } else {
            where.append("and role = ?");
         }
         params.add(user.getRole());
      }
      sql.append(where);

      try {
         return dbHelper.selectTotal(sql.toString(), params.toArray());
      } catch (SQLException e) {
         e.printStackTrace();
         return 0;
      }
   }

   @Override
   public boolean deleteUser(int id) {
      String sql = "delete from kfm_user where id = ?";
      try {
         //删除成功返回false
         boolean update = dbHelper.update(sql, id);
         //System.out.println(update);删除成功为false
         return update;
      } catch (SQLException e) {
         e.printStackTrace();
         return true;
      }
   }

   @Override
   public User selectUserById(int id) {
      String sql = "select id, username,name,phone,role,status,avatar from kfm_user where id = ?";
      try {
         return dbHelper.selectOne(sql, User.class, id);
      } catch (SQLException e) {
         e.printStackTrace();
         return null;
      }
   }

   @Override
   public boolean updateUser(User user) {
      StringBuilder sql = new StringBuilder("update kfm_user set ");
      List params = new ArrayList();
      if (user.getName() != null) {
         sql.append("name = ?, ");
         params.add(user.getName());
      }
      if (user.getPhone() != null) {
         sql.append("phone = ?, ");
         params.add(user.getPhone());
      }
      if (user.getStatus() != null) {
         sql.append("status = ?, ");
         params.add(user.getStatus());
      }
      if (user.getRole() != null) {
         sql.append("role = ?, ");
         params.add(user.getRole());
      }
      if (user.getAvatar() != null) {
         sql.append("avatar = ?, ");
         params.add(user.getAvatar());
      }
      sql.deleteCharAt(sql.length() - 2);

      sql.append(" where id = ?");
      params.add(user.getId());
      System.out.println(sql);

      try {
         //返回false表示执行成功
         return dbHelper.update(sql.toString(), params.toArray());
      } catch (SQLException e) {
         e.printStackTrace();
      }
      return true;
   }
}
